﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
    public class OperateFolderService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 根据文件ID 查找文件信息
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns>文件信息</returns>
        public FileInfo getFileInfoByFileId(int fileId)
        {
            FileInfo fileInfo = new FileInfo();
            using (SqlDataReader reader = db.getDataReader("sp_getFileInfoByFileId", new SqlParameter("@fileId", fileId)))
            {
                if (reader.Read())
                {
                    fileInfo.FilesName = (string)reader[0];
                    fileInfo.FilePath = (string)reader[1];
                    fileInfo.Remark = (string)reader[2];
                    fileInfo.CreateDate = (DateTime)reader[3];
                    fileInfo.FileOwner.LoginId = (string)reader[4];
                }
            }
            db.CloseConnection();
            return fileInfo;
        }

        /// <summary>
        /// 根据登陆名查找登陆ID
        /// </summary>
        /// <param name="loginId">登陆名</param>
        /// <returns>登陆ID</returns>
        public int getUserIdByLoginId(string loginId)
        {
            return (int)db.getScalar("sp_getUserIdByLoginId",new SqlParameter("@loginId",loginId));
        }

        /// <summary>
        /// 根据文件ID来修改文件信息
        /// </summary>
        /// <param name="fileInfo">文件ID</param>
        /// <returns>是否修改成功</returns>
        public bool updateFileInfoByFileId(FileInfo fileInfo)
        {
            SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@fileId",fileInfo.FileId),
                new SqlParameter("@filesName",fileInfo.FilesName),
                new SqlParameter("@remark",fileInfo.Remark)
            };
            if (db.getCount("sp_updateFileInfoByFileId_Folder", values) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据文件ID找这文件的PARENTID
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <returns>返回parentid</returns>
        public int getParentIdByFileId(int fileId)
        {
            return (int)db.getScalar("sp_getParentIdByFileId",new SqlParameter("fileId",fileId));
        }

        /// <summary>
        /// 新增文件信息
        /// </summary>
        /// <param name="fileInfo">要新增的文件信息</param>
        /// <returns>是否新增成功</returns>
        public bool insertFileInfo(FileInfo fileInfo)
        {
            SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@filesName",fileInfo.FilesName),
                new SqlParameter("@filePath",fileInfo.FilePath),
                new SqlParameter("@remark",fileInfo.Remark),
                new SqlParameter("@createDate",fileInfo.CreateDate),
                new SqlParameter("@parentId",fileInfo.ParentId),
                new SqlParameter("@fileOwnerId",fileInfo.FileOwner.UserId)
            };
            if (db.getCount("sp_insertFileInfo_Folder",values)>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
